import axios from 'axios';
import { message } from 'ant-design-vue';
import { useRouter } from "vue-router"
let $router = useRouter()
let baseUrl = import.meta.env.VITE_APP_API_BASE_URL
const instance = axios.create({
    baseURL: baseUrl,
    timeout: 10000
});

//请求拦截器
instance.interceptors.request.use(function (config) {
    // 请求拦截器 - 添加头部token...
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// 响应拦截器
instance.interceptors.response.use(function (response) {
    if(response.data.code == 200 || response.data.success){
        return response.data;
    }else if(response.data.code == 401){
        message.warning('登录过期，请重新登录', 4);
        setTimeout(() => {
            window.replace(import.meta.env.VITE_BACKGROUND_URL)
        }, 4000)
    }else if(response.data.code > 500 || !response.data.success){
        message.warning(response.data.message, 5);
    }
}, function (error) {
    return Promise.reject(error);
});

export default instance
